package jp.co.showbiz.common.base;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;


import org.apache.log4j.Logger;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * クラス概要<br>
 * クラス説明<br>
 * 
 * @version $Revision: 1.1 $
 */
public class BaseDAO {
	/***
	 *logger
	 */
	protected Logger logger = Logger.getLogger(getClass().getName());
	/**
	 * this is reader
	 */
	private Reader reader;
	/**
	 * this is sqlMap
	 */
	public SqlMapClient sqlMap;

	/**
	 * RESOURSE
	 */
	public final String RESOURSE = "/sqlmap.xml";

	/**
	 * this is
	 * 
	 * @throws AppException
	 *             e
	 * @throws IOException
	 *             f
	 * @version 1.0
	 */
	public BaseDAO() throws BaseException, IOException {
		this.reader = Resources.getResourceAsReader(RESOURSE);
		this.sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	}

	@SuppressWarnings("unchecked")
	protected <E> List<E> select(String statementName) throws SQLException {
		return sqlMap.queryForList(statementName);
	}

	@SuppressWarnings("unchecked")
	protected <E> List<E> select(String statementName, Object params) throws SQLException {
		return sqlMap.queryForList(statementName, params);
	}

	@SuppressWarnings("unchecked")
	protected <E> List<E> select(String statementName, Object params, int skipResults, int maxResults) throws SQLException {
		return sqlMap.queryForList(statementName, params, skipResults, maxResults);
	}

	protected Object objSelect(String statementName, Object params) throws SQLException {
		return  (Object)sqlMap.queryForObject(statementName, params);
	}

	protected int update(String statementName, Object params) throws SQLException {
		return (Integer)sqlMap.update(statementName, params);
	}

	protected Object insert(String statementName, Object params) throws SQLException {
		return (Object) sqlMap.insert(statementName, params);
	}

	protected int delete(String statementName, Object params) throws SQLException {
		return (Integer)sqlMap.delete(statementName, params);
	}

}
